package com.group.itms.routes.map;

public class Projection {

	private int mWidth;
	private int mHeight;
	private float mZoomX;
	private float mZoomY;
	private Bounds mBounds;
	private float mZoom;
	
	public Projection (Bounds bounds) {
		mBounds = bounds;
	}
	
	public Projection(int w, int h, Bounds bounds) {
		mWidth = w;
		mHeight = h;
		mBounds = bounds;
	}

	public PointD convertToScreen(PointD location) {
		if (location == null) {
			return null;
		}
		double dx = location.x - mBounds.getLeftDowonX();
		double dy = -(location.y - mBounds.getRightUpY());
		double cX = (dx / getRegionWidth()  * mWidth);
		double cY = (dy / getRegionHeight() * mHeight);
		cX = cX * mZoom + mWidth  / 2f - mZoomX;
		cY = cY * mZoom + mHeight / 2f - mZoomY;
		return new PointD(cX, cY);
	}

	public PointD rawConvertToScreen(PointD location) {
		if (location == null) {
			return null;
		}
		double dx = location.x - mBounds.getLeftDowonX();
		double dy = -(location.y - mBounds.getRightUpY());
		double cX = (dx / getRegionWidth()  * mWidth);
		double cY = (dy / getRegionHeight() * mHeight);
		return new PointD(cX, cY);
	}
	
	public int getWidth() {
		return mWidth;
	}
	
	public void setWidth(int width) {
		mWidth = width;
	}
	
	public int getHeight() {
		return mHeight;
	}
	
	public void setHeight(int height) {
		mHeight = height;
	}
	
	public float getZoomX() {
		return mZoomX;
	}

	public void setZoomX(float zoomX) {
		this.mZoomX = zoomX;
	}

	public float getZoomY() {
		return mZoomY;
	}

	public void setZoomY(float zoomY) {
		this.mZoomY = zoomY;
	}

	public void setBounds(Bounds b) {
		mBounds = b;
	}
	
	public void setZoom(float zoom) {
		mZoom = zoom;
	}
	
	public float getZoom() {
		return mZoom;
	}
	
	private double getRegionWidth() {
		if (mBounds == null) {
			return 0;
		}
		return mBounds.getRightUpX() - mBounds.getLeftDowonX();
	}
	
	private double getRegionHeight() {
		if (mBounds == null) {
			return 0;
		}
		return mBounds.getRightUpY() - mBounds.getLeftDowonY();
	}
}
